class ListNode{
    int val;
    ListNode next;

    ListNode(int val) {
        this.val = val;
        this.next = null;
    }
}

class code2 {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        //1.根据两个结点情况判空，高效返回
        if (list1 == null) return list2;
        if (list2 == null) return list1;
        //2. 新建一个结点，指向结点值较小的那个
        ListNode ret = (list1.val<=list2.val) ? list1:list2;
        ListNode cur = ret;
        ListNode point = null;
        while(list1 != null && list2 != null) {
            if(list1.val <= list2.val) {
                point = list1;
                list1 = list1.next;
            } else {
                point = list2;
                list2 = list2.next;
            }
            if(point.val != cur.val) {
                cur.next = point;
                cur = cur.next;
            }
        }
        //处理多余结点
        while (list1 != null) {
            if(list1.val != cur.val) {
                cur.next = list1;
                cur = cur.next;
            }
            list1 = list1.next;
        }
        while (list2 != null) {
            if(list2.val != cur.val) {
                cur.next = list2;
                cur = cur.next;
            }
            list2 = list2.next;
        }
        //将最后一个结点的下一个结点置位 null
        cur.next = null;
        return ret;
    }
    public static void main(String[] args) {

    }
}